package com.fengziyu.lib_recyclerview_fzy.goods

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.fengziyu.lib_recyclerview_fzy.R
import com.fengziyu.lib_recyclerview_fzy.entity.GoodsEntity
import org.w3c.dom.Text

class GoodsAdapter(private val onItemClick: (GoodsEntity)->Unit , private val onItemLongClick: (GoodsEntity)->Unit): RecyclerView.Adapter<GoodsAdapter.GoodsViewHolder>() {
    private var goodsList: List<GoodsEntity> = emptyList()

    fun setGoodsList(goodsList: List<GoodsEntity>){
        this.goodsList = goodsList
        notifyDataSetChanged()
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GoodsViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_goods, parent, false)
        return GoodsViewHolder(view)
    }

    override fun getItemCount(): Int = goodsList.size

    override fun onBindViewHolder(holder: GoodsViewHolder, position: Int) {
        holder.bind(goodsList[position])
        holder.itemView.setOnClickListener{
            onItemClick(goodsList[position])//点击事件
        }
        holder.itemView.setOnLongClickListener {
            onItemLongClick(goodsList[position])
            true
        }
    }

    inner class GoodsViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        private val nameText:TextView = itemView.findViewById(R.id.desc_goods)
        private val price: TextView = itemView.findViewById(R.id.price_goods)

        private val img: ImageView = itemView.findViewById(R.id.img_goods)
        fun bind(goodsEntity: GoodsEntity) {
            nameText.text = goodsEntity.goods_desc
            price.text = "价格: ${goodsEntity.goods_default_price}"
            Glide.with(itemView.context).load(goodsEntity.goods_default_icon).into(img)
        }

    }


}